﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Xuanren_FrontEnd.Model;

namespace Xuanren_FrontEnd.Areas.Back.Controllers
{
    public class CategoryController : Controller
    {
        private XuanrenTherapyEntities db = new XuanrenTherapyEntities();

        // GET: Category
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Search(int page, int limit, string TypeName, string begin, string end)
        {
             
            {
                var q = db.Category.Where(p => true);

                if (TypeName != "" && TypeName != null)
                {
                    q = q.Where(p => p.TypeName.Contains(TypeName));
                }
                if (!string.IsNullOrWhiteSpace(begin))
                {
                    var b = Convert.ToDateTime(begin);
                    q = q.Where(x => x.create_time >= b);//开始时间
                }
                if (!string.IsNullOrWhiteSpace(end))
                {
                    var e = Convert.ToDateTime(end);
                    q = q.Where(x => x.create_time <= e);//结束时间
                }
                var count = q.Count();
                var list = q.OrderByDescending(x => x.id).Skip((page - 1) * limit).Take(limit).ToList().Select(x => new
                {
                    id = x.id,
                    TypeName = x.TypeName,
                    create_user = x.create_user,
                    create_time = x.create_time.ToString(),
                    update_user = x.update_user,
                    update_time = x.update_time.ToString()
                });
                return Json(new { code = 0, msg = "", count = count, data = list });
            }

        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {
            return View();

        }

        public ActionResult DAdd(Category category)
        {
             
            {
                category.create_time = DateTime.Now;
                category.create_user = Session["username"].ToString();
                category.update_time = DateTime.Now;
                category.update_user = Session["username"].ToString();

                db.Category.Add(category);
                var r = db.SaveChanges();
                if (r > 0)
                {
                    return Json(new { code = 0, msg = "添加成功" });
                }
                else
                {
                    return Json(new { code = 0, msg = "添加失败" });

                }
            }
        }
        public ActionResult Edit(int id)
        {
             
            {
                var category = db.Category.SingleOrDefault(p => p.id == id);
                ViewBag.A = category;
            }
            return View();
        }
        public ActionResult DealEdit(Category category)
        {
             
            {
                var info = db.Category.SingleOrDefault(p => p.id == category.id);
                info.TypeName = category.TypeName;
                category.update_time = DateTime.Now;
                category.update_user = Session["username"].ToString();
                int r = db.SaveChanges();
                if (r > 0)
                {
                    return Json(new { code = 0, msg = "编辑成功" });
                }
                else
                {
                    return Json(new { code = 1, msg = "编辑失败" });
                }
            }

        }

        public ActionResult Delect(int id)
        {
             
            {
                var info = db.Category.SingleOrDefault(p => p.id == id);
                db.Category.Remove(info);
                int r = db.SaveChanges();
                if (r > 0)
                {
                    return Json(new { code = 0, msg = "删除成功" });
                }
                else
                {
                    return Json(new { code = 1, msg = "删除失败" });
                }
            }

        }
    }
}